//
//  SEGTopicDetailWaterfallLayout.m
//  WaterfallDemo
//
//  Created by JokerHe on 2018/4/17.
//  Copyright © 2018年 JokerHe. All rights reserved.
//

#import "SEGTopicDetailWaterfallLayout.h"

#define AutoSize(x) x*LYScreenWidth()/375
static CGFloat const kWordTopicMinTotalHeight = 167.5f;

@interface SEGTopicDetailWaterfallLayout ()

@property (nonatomic, assign) CGFloat contentHeight;
@property (nonatomic, strong) NSMutableArray *attrArray;
@property (nonatomic, strong) NSMutableArray *colHeights;//每列的总高度

@end

@implementation SEGTopicDetailWaterfallLayout

- (instancetype)init {
    if (self = [super init]) {
        self.colCount = 3;
        self.minItemHeight = 20.f;
        
    }
    return self;
}

- (void)prepareLayout {
    [super prepareLayout];
    //清除之前计算的所有高度，因为刷新的时候回调用这个方法
    self.contentHeight = 0;
    self.colHeights = nil;
    self.attrArray = nil;
    
    for (NSInteger section = 0; section < self.collectionView.numberOfSections; section++) {
        
        for (NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:section]; i++) {
            // 创建位置
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:section];
            // 获取indexPath位置cell对应的布局属性
            UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
            [self.attrArray addObject:attrs];
        }
    }
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attrArray;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGFloat w;
    if (indexPath.section == 0) {
        w = LYScreenWidth();
    } else {
        CGFloat contentW = self.collectionView.bounds.size.width - self.sectionInset.left - self.sectionInset.right;
        w = (contentW - (self.colCount - 1) * self.minimumInteritemSpacing) / self.colCount;
    }
    
    CGFloat h = [self.delegate layout:self heightForItemAtIndexPath:indexPath itemWidth:w];
    if (self.colCount != 1 && indexPath.section != 0 && h < w) {
        h = w;
    }
    
    if (indexPath.item == 0) {//本组第一个
        NSInteger destColumn = 0;//当前最高的列
        CGFloat maxColumnHeight = [self.colHeights[0] doubleValue];
        for (NSInteger i = 0; i < self.colHeights.count; i++) {
            CGFloat columnHeight = [self.colHeights[i] doubleValue];
            if (maxColumnHeight < columnHeight) {
                maxColumnHeight = columnHeight;
                destColumn = i;
            }
        }
        for (NSInteger i = 0; i < self.colHeights.count; i++) {
            self.colHeights[i] = @(maxColumnHeight);
        }
    }
    
    NSInteger destColumn = 0;
    CGFloat minColumnHeight = [self.colHeights[0] doubleValue];
    for (NSInteger i = 0; i < self.colHeights.count; i++) {
        CGFloat columnHeight = [self.colHeights[i] doubleValue];
        if (minColumnHeight > columnHeight) {
            minColumnHeight = columnHeight;
            destColumn = i;
        }
    }
    
    CGFloat x = self.sectionInset.left + destColumn * (w + self.minimumInteritemSpacing);
    CGFloat y = minColumnHeight;
    
    if (indexPath.section != 0 && self.colCount != 1 && indexPath.item < self.colCount) {
        y += 5;
    }
    if (y != self.sectionInset.top) {
        y += self.minimumLineSpacing;
    }
    
    attrs.frame = CGRectMake(x, y, w, h);
    
    self.colHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
    
    CGFloat columnHeight = [self.colHeights[destColumn] doubleValue];
    if (self.contentHeight < columnHeight) {
        self.contentHeight = columnHeight;
    }
    return attrs;
}

- (CGSize)collectionViewContentSize {
    CGFloat height = [[self.colHeights firstObject] floatValue];
    for (NSInteger i = 0; i < self.colCount; i++) {
        if ([self.colHeights[i] floatValue] > height) {
            height = [self.colHeights[i] floatValue];
        }
    }
    
    if (self.attrArray.count == 1) {//如果只有头部cell
        if (height > LYScreenHeight() - 300) {
            height += 300;//加上缺省视图的高度
        }
    }
    
    CGSize size = CGSizeMake(0, height + self.sectionInset.bottom);
    if (size.height <= LYScreenHeight()) {
        size.height = LYScreenHeight() + 1;//可滑动
    }
    size.height += LYSafeAreaBottom();
    return size;
}

- (NSMutableArray *)colHeights {
    if (!_colHeights) {
        _colHeights = [NSMutableArray array];
        for (NSInteger i = 0; i < self.colCount; i++) {
            [_colHeights addObject:@0];
        }
    }
    return _colHeights;
}

- (NSMutableArray *)attrArray {
    if (!_attrArray) {
        _attrArray = [NSMutableArray array];
    }
    return _attrArray;
}

@end
